﻿//Copyright (C) <2013><jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@algo-plus.co.uk website <http://www.algo-plus.co.uk/>
using System;
using FinPlusCompCore;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompAlgo
{
    public class AlgPlusVTDirectional : FinPlusComponent
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Test = true)]
        public override string TestString { get { return Adapter.ToString(); } }

        public override IPods Adapter { get { return _adapter; } }
 
        private enum Params { VTDirectional, Ticker }
        private readonly IFinPlusComp _varianceConn, _liveMarketConn, _pxHistoryConn, _regressionConn, _recentAverageConn, _limitsConn;
        private readonly IPods _adapter;
        private readonly string _tickers;

        //construct
        public AlgPlusVTDirectional(IFinPlusComp variance, IFinPlusComp liveMarket, IFinPlusComp pxHistory, IFinPlusComp regression, IFinPlusComp recentAverage, IFinPlusComp limits, string tickers) 
        {
            _adapter = IOC.New<IPods>();
            _varianceConn = variance;
            _liveMarketConn = liveMarket;
            _pxHistoryConn = pxHistory;
            _regressionConn = regression;
            _recentAverageConn = recentAverage;
            _limitsConn = limits;
            _tickers = tickers;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Error.Log(Id, Config, e.Message, e);
            }
        }

        public override void Dispose()
        {
            try
            {
                if (_varianceConn != null)
                    _varianceConn.Dispose();
                if (_liveMarketConn != null)
                    _liveMarketConn.Dispose();
                if (_pxHistoryConn != null)
                    _pxHistoryConn.Dispose();
                if (_regressionConn != null)
                    _regressionConn.Dispose();
                if (_recentAverageConn != null)
                    _recentAverageConn.Dispose();
                if (_limitsConn != null)
                    _limitsConn.Dispose();
            }
            catch (Exception e)
            {
                IsValid = Level.Warning.Log(e.Message, e);
            }
        }

        //private
        private void Load()
        {
            try
            {

                var vt = IOC.New<IPod>(Params.VTDirectional.ToString(), "-1");
                vt[Params.Ticker.ToString()] = _tickers;

                //vt.Append(_varianceConn.Adapter);
                //vt.Append(_regressionConn.Adapter);
                //vt.Append(_recentAverageConn.Adapter);

                _adapter.Add(vt, true, true);
            }
            catch (Exception e)
            {
                IsValid = Level.Error.Log(Id, Config, e.Message, e);
            }
        }
    }
}
